<?php
declare (strict_types=1);

namespace app\common\model;

/**
 * @mixin \think\Model
 */
class Account extends Base
{

    protected $name = 'user_account';

    public function user()
    {
        return $this->hasOne(User::class, 'id', 'uid')->field('id,code,nickname,mobile');
    }

    public function take()
    {
        return $this->hasOne(User::class, 'id', 'source')->field('id,code,nickname,mobile');
    }

    public function getTypeAttr($value)
    {
        return get_pay_way($value);
    }


    public static function _adds($id, $type, $amount, $intros, $fx, $source = 0, $category = 0, $is_grant = 2, $is_bonus = 0, $is_user = 0)
    {
        try {

            if (is_array($intros)) {
                $intro = $intros[0];
                $msg = $intros[1];
            } else {
                $intro = $intros;
                $msg = '';
            }
            $assets = Assets::where('uid', $id)->find();
            $after = $fx == '+' ? floatval($assets->$type) + floatval($amount) : floatval($assets->$type) - floatval($amount);
            parent::create([
                'uid'         => $id,
                'type'        => $type,
                'intro'       => $intro,
                'amount'      => $amount,
                'front'       => $assets->$type,
                'after'       => $after,
                'fx'          => $fx,
                'create_time' => time(),
                'create_date' => date('Y-m-d'),
                'source'      => $source,
                'category'    => $category,
                'is_grant'    => $is_grant,
                'is_bonus'    => $is_bonus,
                'is_user'     => $is_user,
                'is_admin'    => $intro == '后台充值' || $intro == '后台充值' ? 1 : 0,
                'msg'         => $msg
            ]);

            $assets->$type = $after;
            $assets->save();
            return true;
        } catch (\Exception $e) {
            return $e->getMessage();
        }
    }


    public static function getPageList($map = [],
                                       $field = '*',
                                       $page = 1,
                                       $limit = 20,
                                       $merge = true,
                                       $with = [],
                                       $in = [],
                                       $sort = [],
                                       $like = [],
                                       $time = [], $withoutField = '', $append = [])
    {
        try {

            $map = $merge ? array_merge($map, ['status' => 1]) : $map;


            $that = !empty($with) ? parent::with($with)->where($map)->whereTime('create_time', 'month') : parent::where($map)->whereTime('create_time', 'month');
            if (!empty($like)) {
                $that->whereLike($like['field'], '%' . $like['keyword'] . '%');
            }
            if ($time) {
                foreach ($time as $k => $v) {
                    $times = explode(' - ', $v['value']);
                    $that->whereBetweenTime($v['key'], $times[0], $times[1]);
                }
            }
            if (!empty($in)) {
                foreach ($in as $k => $v) {
                    $that->whereIn($k, $v);
                }
            }
            if ($withoutField) {
                $that->withoutField($withoutField);
            } else {
                $that->field($field);
            }
            $count = $that->count();
            $that->page(intval($page), intval($limit));
            if (!$sort) {
                $that->order($merge ? 'sort' : 'create_time', 'desc');
            } else {
                $that->order($sort['key'], $sort['value']);
            }
            $list = $that->select();
            if ($append) {
                $list->append($append);
            }
            $allPage = intval(ceil($count / $limit));
            return ['code' => 1, 'msg' => 'success', 'data' => $list, 'pages' => $allPage];

        } catch (\Exception $e) {

            return ['code' => $e->getCode(), 'message' => $e->getMessage()];

        }
    }


}
